<?php
$source_path = "I:\\mycode\\sim\\chm\\re_codeigniter\\";
$target_path = "I:\\mycode\\sim\\chm\\re_codeigniter\\";



listdir($source_path);

function listdir($source_dir) {
	$sDir = opendir($source_dir);
	$i=0;
	while(false !== ($entry = readdir($sDir))) {
		if($entry !="." && $entry !="..") {
			$i++;
			if(is_dir($source_dir.$entry)) {
				listdir($source_dir.$entry."\\");
			} else {
				//echo $source_dir.$entry."<br />";
				//这里对文件进行处理
//				if($i<=1)
					charsetConvert($source_dir.$entry);
			}
		}
	}
}

function charsetConvert($filename) {
	echo "Start Convert:".$filename."<br />";
	$fp = fopen($filename,"r");
	$str = fread($fp,filesize($filename));
	fclose($fp);
	if(strtolower(mb_detect_encoding($str)) == 'utf-8') {
		$str = mb_convert_encoding($str,"gbk","utf8");
		$search = array("'<script[^>]*?>.*?</script>[\r\n]*?'si","'<link[^>]*?/>'si","'<style[^>]*?>.*?</style>[\r\n]*?'si");
		$replace = array("","","<link href=\"codeigniter.css\" rel=\"stylesheet\" type=\"text/css\" />");
		$str = preg_replace($search,$replace,$str);
		$str = preg_replace_callback('/(<meta.*charset=)(.*)(" \/>)/',create_function('$matches','return $matches[1]."gbk".$matches[3];'),$str);
		//echo $str;
		$fp = fopen($filename,"w");
		fwrite($fp,$str);
	};
	echo "End Convert.<br />";
}
?>